package com.example.homework.ui.note_list;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.homework.EditNoteActivity;
import com.example.homework.MainActivity;
import com.example.homework.R;
import com.example.homework.dao.NoteDao;
import com.example.homework.databinding.FragmentNoteListBinding;
import com.example.homework.pojo.Note;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 这是登陆成功后的主页面，即登陆成功后，直接显示的页面
 */
public class Note_listFragment extends Fragment {

    private FragmentNoteListBinding binding;


    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        MainActivity.instance.finish();//登陆成功后将登录界面的活动杀死。保证在主菜单点击返回时，会返回到桌面而不是登录界面

        Note_listViewModel notelistViewModel =
                new ViewModelProvider(this).get(Note_listViewModel.class);

        binding = FragmentNoteListBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        /**
         * ListView组件测试
         */
        //
        ListView note_list = getActivity().findViewById(R.id.note_listview);//
        NoteDao noteDao = new NoteDao(getContext());

        //获取登录的用户名
        String username = getActivity().getIntent().getStringExtra("uname");

        //使用和获取到的用户名查询
        ArrayList list = noteDao.queryNote(username);
        //调用下面的 ListViewQuery() 方法来将查询到的note内容与id分开
        String[] note = ListViewQuery(list);

//        Toast.makeText(getContext(), list.get(0).toString(), Toast.LENGTH_SHORT).show();
        //设想功能：当noted长度大于一数值时，只取前几个字符，并在其后添加省略号------------------待实现
        /**
         for (int i = 0; i < list.size(); i++) {
         if (list.get(i).toString().length()>10){

         }
         }
         */


        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, note);
        note_list.setAdapter(adapter);

        note_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), EditNoteActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onAttach(@NonNull Activity activity) {
        super.onAttach(activity);
    }

    //将note内容解析出来
    public String[] ListViewQuery(ArrayList<String> list) {
        String[] strings = new String[list.size() / 2];
        for (int i = 0, j = 1; j <= list.size() - 1; i++) {
            strings[i] = list.get(j);
            j += 2;
        }
        return strings;
    }
}